package com.holyn.guofang.view.distinguish;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.holyn.guofang.R;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.BrandBulletinWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.distinguish.SearchResultDetailContract;
import com.holyn.guofang.presenter.distinguish.SearchResultDetailPresenter;
import com.holyn.guofang.view.BaseActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Holyn on 2017/7/19.
 */

public class ProductDetailNoticesAvtivity extends BaseActivity implements BaseView, SearchResultDetailContract.NoticeView{
    private static final int REQUEST_ACTION_POST_DETAIL_NOTICES = 0x0001;

    public static final String INTENT_KEY_MARK_NO = "INTENT_KEY_MARK_NO";
    public static final String INTENT_KEY_MARK_CLASS = "INTENT_KEY_MARK_CLASS";

    @BindView(R.id.tv_summary)
    TextView tvSummary;
    @BindView(R.id.rv_list)
    RecyclerView rvList;

    SearchResultDetailContract.Presenter presenter;

    String MarkNo;
    Integer MarkClass;

    private BrandBulletinWo mBrandBulletinWo;

    public static void start(Activity activity, String MarkNo, Integer MarkClass) {
        Intent intent = new Intent(activity, ProductDetailNoticesAvtivity.class);
        intent.putExtra(INTENT_KEY_MARK_NO, MarkNo);
        intent.putExtra(INTENT_KEY_MARK_CLASS, MarkClass);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.acticity_product_detail_notices;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("商标公告");

        MarkNo = getIntent().getStringExtra(INTENT_KEY_MARK_NO);
        MarkClass = getIntent().getIntExtra(INTENT_KEY_MARK_CLASS, 0);

        tvSummary.setText("第"+MarkClass+"类第"+MarkNo+"号商标的相关公告");

        presenter = new SearchResultDetailPresenter(this);
        presenter.addNoticeView(this);

        presenter.postBulletinInfo(REQUEST_ACTION_POST_DETAIL_NOTICES, MarkNo, MarkClass);

    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(Integer requestAction, BrandBulletinWo brandBulletinWo) {
        if (brandBulletinWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            this.mBrandBulletinWo = brandBulletinWo;
            List<BrandBulletinWo.DataBean.BulletinInfoBean> bulletinInfoBeanList = brandBulletinWo.getData().getBulletinInfo();
            if (bulletinInfoBeanList == null){
                toastMsg("返回数据为空！");
            } else {
                MyRecyclerAdapter recyclerAdapter = new MyRecyclerAdapter(bulletinInfoBeanList);
                View heardView = LayoutInflater.from(ProductDetailNoticesAvtivity.this).inflate(R.layout.item_product_detail_notices, null);
                recyclerAdapter.addHeaderView(heardView);
                rvList.setAdapter(recyclerAdapter);
                rvList.setLayoutManager(new LinearLayoutManager(ProductDetailNoticesAvtivity.this));
                rvList.addItemDecoration(new RecyclerView.ItemDecoration() {
                    @Override
                    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                        super.getItemOffsets(outRect, view, parent, state);
                        outRect.set(0, 0, 0, 1);
                    }
                });
                recyclerAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                        ProductDetailNoticesImagesAvtivity.start(ProductDetailNoticesAvtivity.this, mBrandBulletinWo);
                    }
                });
            }
        } else {
            if (TextUtils.isEmpty(brandBulletinWo.getErr())){
                toastMsg("获取数据失败！");
            } else {
                toastMsg(brandBulletinWo.getErr());
            }
        }

    }

    private class MyRecyclerAdapter extends BaseQuickAdapter<BrandBulletinWo.DataBean.BulletinInfoBean, BaseViewHolder>{

        public MyRecyclerAdapter(@Nullable List<BrandBulletinWo.DataBean.BulletinInfoBean> data) {
            super(R.layout.item_product_detail_notices, data);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, BrandBulletinWo.DataBean.BulletinInfoBean bulletinInfoBean) {
            baseViewHolder.setText(R.id.tv_0, baseViewHolder.getAdapterPosition()+"");
            baseViewHolder.setText(R.id.tv_1, bulletinInfoBean.getPublishDate());
            baseViewHolder.setText(R.id.tv_2, bulletinInfoBean.getIssueNo());
            baseViewHolder.setText(R.id.tv_3, bulletinInfoBean.getPublishName());
        }
    }

}
